<?php
/**
 * 积分查询
 */

namespace App\Controllers\Sms;

use App\Controllers\BaseController;
use App\Library\Helper;
use App\Models\Nc;
use App\Models\SmsWlist;
use App\Models\SmsWlistLog;

class JController extends BaseController {

    protected $smsWlistModel;//积分模型
    protected $smsWlistLogModle;//积分日志模型

    public function initialize() {
        parent::initialize();
        $this->smsWlistModel    = new SmsWlist();
        $this->smsWlistLogModle = new SmsWlistLog();
    }

    /**
     *1.接收到用户上行的消息
     *2.保存用户上行的消息
     *3.判断用户上行的消息
     *4.更新用户回执消息
     */
    public function queryAction() {
        $receiveAt = date('Y-m-d H:i:s', time());//当前时间
        $mobile    = trim($this->request->get('mobile'));
        $text      = trim($this->request->get('text'));
        if(empty($mobile) || empty($text)) {
            echo $this->sendAjax(false, '格式错误');
            exit();
        }

        $moData = [
            'mobile'     => $mobile,  //手机号
            'message'    => $text,    //消息内容
            'msg_id'     => 234234324234,//消息id
            'long_code'  => 7288,//长号码,
            'receive_at' => $receiveAt,
            'create_at'  => $receiveAt,
        ];
        //存储用户上行消息
        $logId = $this->smsWlistLogModle->add($moData);
        //取得当前用户信息
        $ret = $this->smsWlistModel->search($mobile)[0];
        //用户积分
        $integral = $ret['integral'];
        //回执数据id
        $log['id'] = $logId;
        //=================成功=======================
        //关键字符合  并且数据存在
        if(strtoupper($text) == 'JFCX' && !empty($ret)) {
            $log['mt_api_message'] = sprintf('敬的会员，您目前的积分为%d，本条免费，回复TD退订【美赞臣】', $integral);
            $log['mt_status']      = 1;
            //添加回执日志
            $this->smsWlistLogModle->edit($this->mtDate($log));
            //但会成功信息
            $this->success($log['mt_api_message']);
        }
        //=================失败=======================
        //关键字不符合或者符合  但  数据(手机号在白名单)不存在
        else if((strtoupper($text) == 'JFCX' || strtoupper($text) != 'JFCX') && empty($ret)) {
            $log['mt_api_message'] = '您好，您目前还不是会员，请注册后查询，本条免费，回复TD退订【美赞臣】';
            $log['mt_status']      = 0;
            //添加回执日志
            $this->smsWlistLogModle->edit($this->mtDate($log));
            //返回错误信息
            $this->error($log['mt_api_message']);
        }
        else {
            $log['mt_api_message'] = '系统错误，请稍候再试！本条免费，客服电话：010-53358612 回复TD退订【美赞臣】';
            $log['mt_status']      = 0;
            //添加回执日志
            $this->smsWlistLogModle->edit($this->mtDate($log));
            //返回错误信息
            $this->error($log['mt_api_message']);
        }
    }

    /**
     * 成功
     */
    private function success($data) {
        $mes = [
            'status'  => 'true',
            'message' => $data
        ];
        echo json_encode($mes);
        exit();
    }

    /**
     * 失败
     */
    private function error($data) {
        $mes = [
            'status'  => 'false',
            'message' => $data
        ];
        echo json_encode($mes);
        exit();
    }

    /***
     * @param $logId 回执信息
     */
    private function mtDate($log) {
        return $mtData = [
            'id'                => $log['id'],
            'mt_api_message'    => $log['mt_api_message'],
            'mt_api_receive_at' => Helper::getNow(),
            'mt_msg_id'         => '11111111111111111',
            'mt_status'         => $log['mt_status'],
            'mt_at'             => Helper::getNow(),
            'mt_api_receive_at' => Helper::getNow(),
        ];
    }
}